package com.xk.androiddemo.ui.notifications;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;


import com.xk.androiddemo.databinding.FragmentNotificationsBinding;
import com.xk.androiddemo.model.MonitorDataItem;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class NotificationsFragment extends Fragment {

    private FragmentNotificationsBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        NotificationsViewModel notificationsViewModel =
                new ViewModelProvider(this).get(NotificationsViewModel.class);
        binding = FragmentNotificationsBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        notificationsViewModel.data.observe(getActivity(), new Observer<MonitorDataItem>() {
            @Override
            public void onChanged(MonitorDataItem monitorDataItem) {
                binding.tvPress.setText(monitorDataItem.getGasPressure()+"");
                binding.tv2.setText(monitorDataItem.getFlowRate()+"");
                binding.tv3.setText(monitorDataItem.getCurrent()+"");
                if(monitorDataItem.isAlarmSet()){
                    binding.tv4.setText("开");
                }else{
                    binding.tv4.setText("关");
                }
                binding.tv5.setText(monitorDataItem.getFlowRate()+"");
                binding.tv6.setText(monitorDataItem.getVoltage()+"");
                if(monitorDataItem.getValvePosition()){
                    binding.tv7.setText("开");
                }else{
                    binding.tv7.setText("关");
                }
            }
        });
        LocalDateTime currentTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = currentTime.format(formatter);
        binding.tvTime.setText(formattedDateTime);
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}